package org;

import java.util.ArrayList;

import org.entities.LevelObject;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Color;
import org.newdawn.slick.Input;
import org.newdawn.slick.Image;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Font;

public class CtrlState  extends BasicGameState {

    private StateBasedGame game;
    private Image logo,titlebg,screen,sndVol;
    private ArrayList <String>menus;
    private int selected;
    private boolean left,right,enter;
    private Font font;
    private int menuScroll;
    private Sound menuSelect;
    public static final int ID = 1;    
    public int getID() {
        return ID;
    }

    public void init(GameContainer container, StateBasedGame game) throws SlickException {

        this.game = game;		
        titlebg = new Image("data/image/titlebg.jpg");
        logo = new Image("data/image/sweaselLogo.png");
        font = new AngelCodeFont("data/font/weasel.fnt","data/font/weasel.png");
        screen = new Image("data/image/ctrlScreen3.png");
        sndVol = new Image("data/image/sndVol.png");
        menuSelect=new Sound("data/sound/Click3.ogg");

        menus = new ArrayList<String>();
        menus.add("Music  :");
        menus.add("Sound  :");  
        menus.add("Arrows :    movements");
        menus.add("X key  :    jump");
        menus.add("V key  :    boost");
        menus.add("C key  :    shoot");
        menus.add("Enter  :    action");
        menus.add("LShift :    map");
        menus.add("ESC    :    title");
        menus.add("F key  :    fullscreen");
        menus.add("Space  :    show health");
        menus.add("Tab    :    Status screen");
    }

    public void update(GameContainer container, StateBasedGame game, int delta){

        if (selected==0){
            if (left) {
                if (Progress.musicVol>0)
                    Progress.musicVol-=0.01*delta;
                Progress.musModified=true;
            }   
            if (right) {
                if ( Progress.musicVol<20)
                    Progress.musicVol+=0.01*delta;
                Progress.musModified=true;
            } 
        }

        if (selected==1){
            if (left) {
                if (Progress.sfxVol>0)
                    Progress.sfxVol-=0.01*delta;
            }   
            if (right) {
                if ( Progress.sfxVol<20)
                    Progress.sfxVol+=0.01*delta;
            } 
        }        
        
        if (Progress.musModified){
            TitleState.titleMusic.setVolume(Progress.musicVol/20);
        }
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) {
        if (!LevelObject.debugMode)
        g.scale(2f, 2f);
        g.drawImage(titlebg,0,0);
        g.drawImage(logo,200-(logo.getWidth()/2),50-(logo.getHeight()/2));
        g.drawImage(screen, 88, 112);
        g.scale(0.8f, 0.8f);
        g.setColor(Color.white);
        g.drawString(Progress.version, 420, 5);
        g.resetTransform(); 
        
        
        g.setFont(font);
        //g.setColor(Color.red);
        //g.drawRect(186, 240, 428, 254);
        g.setClip(186,240,428,254);
        if (!LevelObject.debugMode)
        g.scale(2f, 2f);
        g.setColor(Color.green);
        g.fillRect(185, 128+menuScroll, (int)Progress.musicVol*4, 6);
        g.drawImage(sndVol, 185, 128+menuScroll);

        g.setColor(Color.green);
        g.fillRect(185, 142+menuScroll,(int)Progress.sfxVol*4 , 6);
        g.drawImage(sndVol, 185, 142+menuScroll);

        for (int i=0;i<menus.size();i++) {
            if (selected == i) {
                g.setColor(Color.white);
                menus.get(i);
                g.drawString(menus.get(i), 100, (122+menuScroll)+i*15);
            }
            else{
                g.setColor(Color.gray);
                menus.get(i);
                g.drawString(menus.get(i), 100, (122+menuScroll)+i*15);
            }
        }        
        g.clearClip();
        if (selected>=2&&selected<=10){
            if (enter){
                g.setColor(Color.white);
                g.drawString("keys can't be customized yet",100,265);
            }
        }
    }


    public void keyPressed(int key, char c) {

        if (key == Input.KEY_LEFT) {
            left=true;
        }
        if (key == Input.KEY_RIGHT) {
            right=true;
        }

        if (key == Input.KEY_DOWN) {
            menuSelect.play(1,Progress.sfxVol/20);
            selected++;
            if (selected >= menus.size()) {
                selected = menus.size()-1;
            }
            if (selected>2&&!(selected>=menus.size()-1)){
                menuScroll-=16;
            }
        }
        if (key == Input.KEY_UP) {
            menuSelect.play(1,Progress.sfxVol/20);
            selected--;
            if (selected < 0) {
                selected = 0;
            }
            if (selected<menus.size()-2&&!(selected<=1)){
                menuScroll+=16;
            }
        }
        if (key == Input.KEY_ENTER) {
            enter=true;
        }

    }    

    public void keyReleased(int key, char c) {
        if (key == Input.KEY_LEFT) {
            left=false;
        }
        if (key == Input.KEY_RIGHT) {
            right=false;
        }
        if (key == Input.KEY_ENTER) {
            enter=false;
        }
        
        if (key == Input.KEY_ESCAPE) {			
            game.enterState(TitleState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));			
        }

    }
}
